//
//  LoginViewController.m
//  AmountBody
//
//  Created by smm on 2017/12/5.
//  Copyright © 2017年 RCMTM. All rights reserved.
//

#import "LoginViewController.h"
#import "HomeViewController.h"


#define Text_Tag 10000
@interface LoginViewController ()<UITextFieldDelegate>
//背景图片
@property (weak, nonatomic) IBOutlet UIImageView *backImage;
//顶部图片
@property (weak, nonatomic) IBOutlet UIImageView *headImage;

//登录信息View
@property (weak, nonatomic) IBOutlet UIView *newsView;
@property (weak, nonatomic) IBOutlet UIView *acountView;
@property (weak, nonatomic) IBOutlet UIImageView *acountImage;
@property (weak, nonatomic) IBOutlet UITextField *acountText;
@property (weak, nonatomic) IBOutlet UIView *acountLine;

@property (weak, nonatomic) IBOutlet UIView *passwordView;
@property (weak, nonatomic) IBOutlet UIImageView *passwordImage;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UIView *passwordLine;
@property (weak, nonatomic) IBOutlet UIView *codeView;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UIView *codeLine;
@property (weak, nonatomic) IBOutlet UIButton *codeButton;
- (IBAction)codeButton:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UIButton *loginButton;
- (IBAction)loginButton:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *languageButton;
- (IBAction)languageButton:(UIButton *)sender;
//底部label
@property (weak, nonatomic) IBOutlet UILabel *downLabel;
//遮罩层
@property (weak, nonatomic) IBOutlet UIView *zheView;
@property (weak, nonatomic) IBOutlet UIImageView *zheImage;

@property (assign, nonatomic) BOOL isChinese;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString * language = [UserManager language];
    if (language.length >0 && [language isEqualToString:@"en"]) {
        [GDLocalizableController setUserlanguage:language];
        _isChinese = NO;
        
    }else{
        [GDLocalizableController setUserlanguage:CHINESE];
        _isChinese = YES;
    }
    
    
    [self loadUI];
    
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.public_headView.hidden = YES;
   
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (_isHome) {
        [self loadAnimation];
//        [self loadNewAnimation];
    }
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    CGFloat imageW = 180;
    CGFloat imageH = 122;
    if (!_isHome) {
        [_zheImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            layoutViews(^{
                _backImage.image = [UIImage imageNamed:@"@backgroud"];
                make.centerX.equalTo(self.view);
                make.top.equalTo(self.view).offset(80);
                make.width.equalTo(@(imageW));
                make.height.equalTo(@(imageH));
            }, ^{
                _backImage.image = [UIImage imageNamed:@"@backgroud2"];
                make.centerX.equalTo(self.view);
                make.top.equalTo(self.view).offset(160);
                make.width.equalTo(@(imageW));
                make.height.equalTo(@(imageH));
            });
        }];
    }
}
#pragma mark -  UI布局

-(void)loadUI
{
//    _backImage.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H);
    [_backImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    _backImage.image = [UIImage imageNamed:@"@backgroud"];
    CGFloat imageW = 180;
    CGFloat imageH = 122;
    CGFloat viewW= 360;
    CGFloat viewH = 30;
    CGFloat viewJG = 80-viewH;
//    CGFloat newsH =(viewH+viewJG)*3+30*2+80+imageH;
    _headImage.image = [UIImage imageNamed:@"@logo"];
    _headImage.hidden = YES;
    _newsView.backgroundColor = [UIColor clearColor];

    [_newsView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(@(viewW));
        make.height.equalTo(@((viewH+viewJG)*3+30*2));
        make.top.equalTo(_zheImage.mas_bottom).offset(80);
    }];
    //账号

    [_acountView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(_newsView);
        make.height.equalTo(@(viewH));
    }];
    _acountView.backgroundColor = [UIColor clearColor];
    CGFloat l_imageW = 15;
    CGFloat l_imageH = 18;
    CGFloat l_jianGeW = 5;
    _acountImage.frame = CGRectMake(0, (viewH-l_imageH)/2.0, l_imageW, l_imageH);
    [_acountImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_acountView);
        make.width.equalTo(@(l_imageW));
        make.centerY.equalTo(_acountView);
        make.height.equalTo(@(l_imageH));
    }];
    _acountImage.image = [UIImage imageNamed:@"@person"];
    NSString * username = GDLocalizedString(@"user_name");
    _acountText.placeholder = username;
    _acountText.delegate = self;
    _acountText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:username attributes:@{NSForegroundColorAttributeName: RGBColorAlpha(155, 155, 155, 1)}];
    _acountText.tag = Text_Tag;
    _acountText.textColor = [UIColor whiteColor];
    _acountText.font = [UIFont systemFontOfSize:12];

    [_acountText mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_acountImage.mas_right).offset(l_jianGeW);
        make.right.equalTo(_acountView);
        make.width.equalTo(@(l_imageW));
        make.centerY.equalTo(_acountView);
        make.height.equalTo(@(Nomal_Label_H));
    }];
    [_acountLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(_acountView);
        make.height.equalTo(@(1));
        
    }];
    _acountLine.backgroundColor = RGBColorAlpha(155, 155, 155, 1);
    //密码
    [_passwordView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_newsView);
        make.top.equalTo(_acountView.mas_bottom).offset(viewJG);
        make.height.equalTo(@(viewH));
    }];
    _passwordView.backgroundColor = [UIColor clearColor];
    _passwordImage.image = [UIImage imageNamed:@"@key"];
    [_passwordImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_passwordView);
        make.width.equalTo(@(l_imageW));
        make.centerY.equalTo(_passwordView);
        make.height.equalTo(@(l_imageH));
    }];
    NSString * password = GDLocalizedString(@"password");
    _passwordText.placeholder = password;
    _passwordText.delegate = self;
    _passwordText.tag = Text_Tag+1;
    _passwordText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:password attributes:@{NSForegroundColorAttributeName: RGBColorAlpha(155, 155, 155, 1)}];
    _passwordText.textColor = [UIColor whiteColor];
    _passwordText.font = [UIFont systemFontOfSize:12];
    [_passwordText mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_passwordImage.mas_right).offset(l_jianGeW);
        make.right.equalTo(_passwordView);
        make.width.equalTo(@(l_imageW));
        make.centerY.equalTo(_passwordView);
        make.height.equalTo(@(Nomal_Label_H));
    }];

    _passwordLine.backgroundColor = RGBColorAlpha(155, 155, 155, 1);
    [_passwordLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(_passwordView);
        make.height.equalTo(@(1));
        
    }];
    //验证码

    [_codeView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_newsView);
        make.top.equalTo(_passwordView.mas_bottom).offset(viewJG);
        make.height.equalTo(@(viewH));
    }];
    _codeView.backgroundColor = [UIColor clearColor];
    NSString * code =GDLocalizedString(@"photo_code");
    _codeText.placeholder = code;
    _codeText.tag = Text_Tag+2;
    _codeText.delegate = self;
    _codeText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:code attributes:@{NSForegroundColorAttributeName: RGBColorAlpha(155, 155, 155, 1)}];
    _codeText.textColor = [UIColor whiteColor];
    _codeText.tintColor = [UIColor whiteColor];
    _codeText.font = [UIFont systemFontOfSize:12];
    _codeText.frame = CGRectMake(_passwordImage.frame.origin.x+_passwordImage.frame.size.width+l_jianGeW, viewJG+(viewH-Nomal_Label_H)/2.0, 200-(_passwordImage.frame.origin.x+_passwordImage.frame.size.width+l_jianGeW), Nomal_Label_H);
    [_codeText mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_passwordImage.mas_right).offset(l_jianGeW);
        make.right.equalTo(_codeView);
        make.width.equalTo(@(l_imageW));
        make.centerY.equalTo(_codeView);
        make.height.equalTo(@(Nomal_Label_H));
    }];

    [_codeLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(_codeView);
        make.right.equalTo(_codeButton.mas_left);
        make.height.equalTo(@(1));
        
    }];
    _codeLine.backgroundColor = RGBColorAlpha(155, 155, 155, 1);
    CGFloat buttonW = 150;
    CGFloat buttonH = 50;
    

    [_codeButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_codeView);
        make.width.equalTo(@(buttonW));
        make.height.equalTo(@(buttonH));
        make.bottom.equalTo(_codeView);
    }];
    _codeButton.backgroundColor = RGBColorAlpha(85, 85, 85, 0.6);
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor], NSKernAttributeName:@5.5f};
  
    NSString * title = [self codeRandom];
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:title attributes:dic];
    [_codeButton setAttributedTitle:attributeStr forState:UIControlStateNormal];
    [_codeButton setAttributedTitle:attributeStr forState:UIControlStateSelected];
    
    [_codeButton setYuanJiaoForRadius:7.0 andMasks:NO];
    buttonW = 360;
//    _loginButton.frame =CGRectMake(0, _codeView.frame.origin.y+_codeView.frame.size.height+80, buttonW, buttonH);
    [_loginButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_newsView);
        make.height.equalTo(@(buttonH));
        make.top.equalTo(_codeView.mas_bottom).offset(80);
    }];
    [_loginButton setTitle:GDLocalizedString(@"sign_in") forState:UIControlStateNormal];
    [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _loginButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_loginButton setYuanJiaoForRadius:buttonH/2.0 andMasks:NO];
    _loginButton.backgroundColor = RGBColorAlpha(255, 66, 89, 1);
    _downLabel.text = GDLocalizedString(@"login_content");
    _downLabel.font = [UIFont systemFontOfSize:12];
    _downLabel.textColor = RGBColorAlpha(170, 170, 170, 1);
    _downLabel.numberOfLines = 2;
    _downLabel.textAlignment = NSTextAlignmentCenter;
    [_downLabel sizeToFit];
    [_downLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-8);
        make.height.equalTo(@(Nomal_Label_H));
    }];
    CGFloat jianGe = 30;
    buttonW = 48;
    buttonH = 47;
    
    [_languageButton setBackgroundImage:[UIImage imageNamed:@"@hand"] forState:UIControlStateNormal];
    [_languageButton setBackgroundImage:[UIImage imageNamed:@"@hand"] forState:UIControlStateSelected];
    [_languageButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    if (_isChinese) {
        [_languageButton setTitle:@"中文" forState:UIControlStateNormal];
        [GDLocalizableController setUserlanguage:CHINESE];
    }else{
        [_languageButton setTitle:@"English" forState:UIControlStateNormal];
        [GDLocalizableController setUserlanguage:ENGLISH];
        
    }
    [self.view bringSubviewToFront:_zheView];
    [self.view bringSubviewToFront:_zheImage];

    [_zheView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    if (_isHome) {
        imageW = 216;
        imageH = imageW/180*122;
        _newsView.hidden = YES;
        _zheView.hidden = NO;

        [_zheImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            
            make.width.equalTo(@(imageW));
            make.height.equalTo(@(imageH));
            layoutViews(^{
                make.centerX.equalTo(self.view).offset(-7);
                make.centerY.equalTo(self.view);
            }, ^{
                make.centerX.equalTo(self.view);
                make.centerY.equalTo(self.view).offset(-80);
            });
            

        }];

        [_languageButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_right);
            make.top.equalTo(@(jianGe));
            make.width.equalTo(@(buttonW));
            make.height.equalTo(@(buttonH));

        }];
    }else{
        _newsView.hidden = NO;
        _zheView.hidden = YES;

        [_zheImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.view).offset(80);
            make.width.equalTo(@(imageW));
            make.height.equalTo(@(imageH));
            
        }];

        [_languageButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_right).offset(-(jianGe+buttonW));
            make.top.equalTo(@(jianGe));
            make.width.equalTo(@(buttonW));
            make.height.equalTo(@(buttonH));
            
        }];
    }
    _languageButton.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    _languageButton.titleLabel.font = Cell_Medium_Font;
   
    _zheImage.image = [UIImage imageNamed:@"@logo"];
    _zheView.backgroundColor = RGBColorAlpha(54, 54, 54, 0.99);
}
-(void)loadAnimation
{
    CGFloat imageW = 180;
    CGFloat imageH = 122;
    CGFloat jianGe = 30;
    CGFloat buttonW = 48;
    CGFloat buttonH = 47;
    _isHome = NO;
    [_zheImage.superview layoutIfNeeded];
    [UIView animateWithDuration:1.0f delay:1.5 options:UIViewAnimationOptionCurveEaseIn animations:^{
        _zheView.backgroundColor = RGBColorAlpha(0, 0, 0, 0);
        [_zheImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.width.equalTo(@(imageW));
            make.height.equalTo(@(imageH));
            layoutViews(^{
                make.top.equalTo(self.view).offset(80);
                
            }, ^{
                make.top.equalTo(self.view).offset(160);
                
            });
        }];
        
        [_zheImage.superview layoutIfNeeded];

    } completion:^(BOOL finished) {
        _newsView.hidden = NO;
        _zheView.hidden = YES;
        [_languageButton.superview layoutIfNeeded];
        [UIView animateWithDuration:0.47 delay:0.3 options:UIViewAnimationOptionCurveEaseIn animations:^{
            [_languageButton mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view.mas_right).offset(-(jianGe+buttonW));
                make.top.equalTo(@(jianGe));
                make.width.equalTo(@(buttonW));
                make.height.equalTo(@(buttonH));
                
            }];
            [_languageButton.superview layoutIfNeeded];
        } completion:^(BOOL finished) {
            
        }];
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


#pragma mark - textFeildDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    int index = (int)textField.tag-Text_Tag;
    if (textField.becomeFirstResponder) {
        UITextField * text = [_newsView viewWithTag:Text_Tag+index+1];
        [textField resignFirstResponder];
        [text becomeFirstResponder];
    }
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{
   
    return YES;
    
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    
    return YES;
}

#pragma mark - 其他方法
//block
static LoginBlcok _loginBlock;
+(void)blockForLogin:(LoginBlcok)loginBlock
{
    _loginBlock = loginBlock;
}
//验证码随机数
-(NSString*)codeRandom
{
    NSArray * countArr = [[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];
    NSString * title = @"";
    for(NSInteger i = 0; i < 5; i++)
    {
        NSInteger index = arc4random() % ([countArr count] - 1);
        NSString *oneText = [countArr objectAtIndex:index];
        title = (i==0)?oneText:[title stringByAppendingString:oneText];
    }
    return title;
}

#pragma mark -  手势按钮方法
- (IBAction)codeButton:(UIButton *)sender {
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor], NSKernAttributeName:@5.5f};
    NSString * title = [self codeRandom];
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:title attributes:dic];
    [_codeButton setAttributedTitle:attributeStr forState:UIControlStateNormal];
    [_codeButton setAttributedTitle:attributeStr forState:UIControlStateSelected];
}
- (IBAction)loginButton:(UIButton *)sender {
    if (_loginBlock) {
        _loginBlock(YES);
    }
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}
- (IBAction)languageButton:(UIButton *)sender {
    _isChinese = !_isChinese;
    if (_isChinese) {
        [_languageButton setTitle:@"中文" forState:UIControlStateNormal];
        [GDLocalizableController setUserlanguage:CHINESE];
        [UserManager saveLanguage:CHINESE];
    }else{
        [_languageButton setTitle:@"English" forState:UIControlStateNormal];
        [GDLocalizableController setUserlanguage:ENGLISH];
        [UserManager saveLanguage:ENGLISH];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeLanguage" object:nil];
    _isHome = NO;
    [self loadUI];
}
@end
